package com.zhugang.week03;

/**
 * @program algorithms
 * @description: tripleInOne
 * @author: chanzhugang
 * @create: 2022/06/08 14:49
 */
public class TripleInOne2 {

    private final int[] array;
    private final int n;
    private final int[] top;


    /**
     * 三合一： 用一个数组实现三个栈
     * <p>
     * 对比ArrayStack(一个数组实现一个栈)
     *
     * @param stackSize
     */
    public TripleInOne2(int stackSize) {
        array = new int[stackSize * 3];
        n = stackSize * 3;
        top = new int[3];
        top[0] = -3;
        top[1] = -2;
        top[2] = -1;
    }

    public void push(int stackNum, int value) {
        if (top[stackNum] + 3 >= n) {
            // 是否满了
            return;
        }
        top[stackNum] += 3;
        array[top[stackNum]] = value;
    }

    public int pop(int stackNum) {
        if (top[stackNum] < 0) {
            return -1;
        }
        int res = array[top[stackNum]];
        top[stackNum] -= 3;
        return res;
    }

    public int peek(int stackNum) {
        if (top[stackNum] < 0) {
            return -1;
        }
        return array[top[stackNum]];
    }

    public boolean isEmpty(int stackNum) {
        return top[stackNum] < 0;
    }
}